package com.northpool.operator.common.crypto;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;

import java.nio.charset.StandardCharsets;


@OperatorInfo(
        ref = "EncryptOperator",
        name = "加密",
        version = "DESede",
        description = "字符串加密算子"
)
@OutputParams({
        @OutputParam(ref = "DESede", name = "DESede值", desc = "字符串的DESede值")
})
public class DESedeEncryptOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "str", name = "待加密字符串", desc = "待DESede加密字符串", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "key", name = "密钥", desc = "密钥", type = TypeEnum.STRING, required = true)
    })
    public DESedeEncryptOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(DESedeEncryptOperator.class, TestParamBuilder.aBuilder()
                .param("str", "123456")
                .param("key", "k����\u0010>\u000E��=�k����\u0010>\u000E")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String str = paramTool.getStr("str");
        String key = paramTool.getStr("key");

        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.DESede, key.getBytes(StandardCharsets.UTF_8));
        String encryptHex = aes.encryptHex(str);

        this.result("DESede", encryptHex);
    }
}
